<?php

/**
 * This is the model class for table "tbl_logo".
 *
 * The followings are the available columns in table 'tbl_logo':
 * @property integer $logo_id
 * @property string $logo_image
 * @property integer $logo_status
 */
class Logo extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_logo';
	}
    
    /**
	 * @return string logo file name, get current logo 
	 */
    public function getLogo() {         
        $logo_image=$this->find("logo_status=1");
        return $logo_image['logo_image'];
    }
    
    /**
	 * @return string logo's filename
	 */
    public function getLogoFile($logo_id) {         
        $logo_image=$this->findByPk($logo_id);
        return $logo_image['logo_image'];
    }
    
    /**
    *@return boolean delete logo filename
    */
    public function delLogoFileName($id){
        $filename=Logo::model()->getLogoFile($id);
        if(file_exists(str_replace(DOMAIN,'',IMG_LOGO_FOLDER).$filename)){
            chmod(str_replace(DOMAIN,'',IMG_LOGO_FOLDER).$filename,0755);
            if(!unlink(str_replace(DOMAIN,'',IMG_LOGO_FOLDER).$filename)){
                return false;
            }else{
                return true;
            }
        }else{
            return false;
        }
    }
    
    /**
	 * @return active logo
	 */
    public function setActiveLogo($logo_id=1) {
        self::deactiveAllLogo();
        return $this->updateByPk($logo_id,array("logo_status"=>1));
    }
    
    public function deactiveAllLogo() {                
        return $this->updateAll(array("logo_status"=>0),"logo_status!=0");
    }

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('logo_image, logo_status', 'required','message'=>REQUIRED_NOT_NULL),
            array('logo_image','file','maxSize'=>6388608,'types'=>'jpg,jpeg,gif,png','wrongType'=>WRONG_TYPE,'tooLarge'=>'Giới hạn dung lượng 6Mb'),
			array('logo_status', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('logo_id, logo_image, logo_status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'logo_id' => 'Mã',
			'logo_image' => 'Ảnh',
			'logo_status' => 'Trạng thái',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('logo_id',$this->logo_id);
		$criteria->compare('logo_image',$this->logo_image,true);
		$criteria->compare('logo_status',$this->logo_status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Logo the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
